const client = require('./theme-color-replacer/client')
//const generate = require('@ant-design/colors/dist/src/generate').default
import { generate } from '@ant-design/colors';
import ITheme from '@/api/entity/ITheme';
import IThemeModifyVars from '@/api/entity/IThemeModifyVars';
import message from 'ant-design-vue/es/message'
import store from '@/store/index';
import ThemeConfig from '@/config/ThemeConfig';

export default class ThemeColor {
    public getAntdSerials(color: string): Array<string> {
        // 淡化（即less的tint）
        const lightens = new Array(9).fill("").map((t, i) => {
            return client.varyColor.lighten(color, i / 10)
        })
        console.log(lightens, "lightens");
        // colorPalette变换得到颜色值
        const colorPalettes = generate(color)
        const rgb = client.varyColor.toNum3(color.replace('#', '')).join(',')
        console.log(colorPalettes, rgb, "当前主色");
        return lightens.concat(colorPalettes).concat(rgb)
    }
    public changeColor(newColor: Array<string>): void {
        var options = {
            newColors: newColor, // new colors array, one-to-one corresponde with `matchColors`
            changeUrl(cssUrl: string) {
                return `/${cssUrl}` // while router is not `hash` mode, it needs absolute path
            }
        }
        return client.changer.changeColor(options, Promise)
    }

    public changeAttrColr(index:number): void {
        let theme:ITheme = new ThemeConfig().getTheme()[index];
        const hideMessage = message.loading('正在切换主题！', 0);
        let colorVars: any = { "@primary-color": theme.color };
        if (theme.modifyVars) {
            for (var i = 0; i < theme.modifyVars.length; i++) {
                colorVars[theme.modifyVars[i].attrName] = theme.modifyVars[i].attrValue;
            }
        }
        (window as any).less.modifyVars(colorVars).then(() => {
            store.dispatch("themeIndex" , index);
            setTimeout(() => {
                hideMessage()
            }, 10)
        });
    }
}